<template>
  <div class="container">
    <div
      v-if="!isLogin"
      class="login-box"
      v-loading="loading"
      element-loading-text="登录中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <div class="login-tit">
        <img src="../assets/imgs/login_icon.png" alt="" />
        受众分析系统
      </div>

      <div class="item">
        <p class="s1">账号</p>
        <el-input
          placeholder="请输入账号"
          v-model="userData.username"
          clearable
        >
        </el-input>
      </div>
      <div class="item">
        <p class="s1">密码</p>
        <el-input
          placeholder="请输入密码"
          v-model="userData.password"
          clearable
          type="password"
        >
        </el-input>
      </div>
      <div class="login-sub" @click="login">登录</div>
    </div>
    <div
      v-else
      class="login-box"
      v-loading="loading"
      element-loading-text="登录中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <div class="login-tit">
        <img src="../assets/imgs/login_icon.png" alt="" />
        受众分析系统
      </div>
      <p class="txt">账号：{{ "user_01" }}</p>
      <div class="item">
        <p class="s1">新密码</p>
        <el-input placeholder="请输入新密码" v-model="newpassword" clearable>
        </el-input>
      </div>
      <div class="item">
        <p class="s1">确认密码</p>
        <el-input
          placeholder="再次输入新密码"
          v-model="secondnewpassword"
          clearable
          type="password"
        >
        </el-input>
      </div>
      <div class="login-sub" @click="setPassWord">登录</div>
    </div>
  </div>
</template>
<script>
import { Login, InsertUser } from "@/api/login";
import { tipSuc, tipErr } from "@/utils/index";
export default {
  name: "login",
  data() {
    return {
      isLogin: false,
      loading: false,
      userData: {
        username: "",
        password: "",
      },
      newpassword: "",
      secondnewpassword: "",
    };
  },
  computed: {
    userInfo: function() {
      return this.$store.userInfo;
    },
  },
  watch: {
    userInfo: function(nVal) {
      if (this.$store.userInfo) {
        this.isLogin = true;
      }
    },
  },
  created() {
    this.isLogin = this.$store.userInfo ? true : false;
  },
  methods: {
    async login() {
      if (!this.userData.username.trim()) {
        tipErr("请输入账号名称");
        this.loading = false;
        return;
      } else if (!this.userData.password.trim()) {
        tipErr("请输入密码");
        this.loading = false;
        return;
      }
      this.loading = true;

      let {data,code,msg} = await Login(this.userData);
      if(code == 1){
        let userInfo = {name:data.name}
        this.$store.commit('setUserInfo',userInfo)
        sessionStorage.setItem('userInfo',JSON.stringify(userInfo))
        this.$router.push({
          name:'index'
        })
      }else{
        tipErr(msg)
      }
      this.loading = false;
    },
    async setPassWord() {
      if (!this.newpassword.trim()) {
        tipErr("请输入密码");
        this.loading = false;
        return;
      } else if (!this.secondnewpassword.trim()) {
        tipErr("请输入确认密码");
        this.loading = false;
        return;
      }
      this.loading = true;

      // let { data } = await Login(this.userData);
      // tipSuc("成功");
      this.loading = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  max-width: 1920px;
  height: 100vh;
  margin: 0 auto;
  background: url("../assets/imgs/login_bg.png") no-repeat;
  background-position: calc(100% - 90px) center;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  .login-box {
    margin-left: 308px;
    width: 420px;
    .txt {
      font-size: 16px;
      color: #eeeeee;
      margin-bottom: 30px;
      padding-left: 16px;
    }
    .login-tit {
      font-size: 28px;
      color: #fff;
      text-align: center;
      line-height: 38px;
      margin-bottom: 66px;
      img {
        width: 44px;
        height: 38px;
        vertical-align: middle;
        margin-top: -5px;
      }
    }
    .item /deep/ {
      margin-bottom: 30px;
      p {
        font-size: 16px;
        color: #bbb;
        line-height: 20px;
        padding-left: 16px;
        margin-bottom: 15px;
      }
      .el-input__inner {
        height: 54px;
        line-height: 54px;
        background: transparent;
        border: 1px solid #3d434c;
        color: #fff;
        &::-webkit-input-placeholder {
          font-size: 14px;
          color: #525966;
        }
      }
    }
    .login-sub {
      margin-top: 50px;
      width: 100%;
      height: 54px;
      line-height: 54px;
      text-align: center;
      font-size: 16px;
      color: #fff;
      background: #cc7029;
      box-shadow: 0px 20px 20px 0px rgba(255, 78, 0, 0.1);
      cursor: pointer;
    }
  }
}
</style>
